package com.daichi.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSON;
import com.daichi.yygh.hosp.repository.DepartmentRepository;
import com.daichi.yygh.hosp.service.DepartmentService;
import com.daichi.yygh.model.hosp.Department;
import com.daichi.yygh.vo.hosp.DepartmentQueryVo;
import com.daichi.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    private final DepartmentRepository departmentRepository;

    @Autowired
    public DepartmentServiceImpl(DepartmentRepository departmentRepository) {
        this.departmentRepository = departmentRepository;
    }


    @Override
    public void save(Map<String, Object> paramMap) {
        Department department = JSON.parseObject(JSON.toJSONString(paramMap), Department.class);

        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (departmentExist != null) {
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        } else {
            department.setIsDeleted(0);
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> findDepartmentPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建Pageable对象，页数从0开始，所以-1
        Pageable pageable = PageRequest.of(page - 1, limit);

        // 创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        Page<Department> departmentPage = departmentRepository.findAll(example, pageable);

        return departmentPage;

    }

    @Override
    public void remove(String hoscode, String depcode) {

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }

    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {

        List<DepartmentVo> resultList = new ArrayList<>();

        // 根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        Example<Department> example = Example.of(departmentQuery);
        List<Department> departmentList = departmentRepository.findAll(example);

        // 根据大科室编码分组，封装返回列表
        Map<String, List<Department>> listMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : listMap.entrySet()) {
            List<Department> deptList = entry.getValue();

            // 封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(entry.getKey());
            departmentVo.setDepname(deptList.get(0).getBigname());
            departmentVo.setChildren(new ArrayList<>());

            // 封装小科室
            deptList.forEach(dept -> {
                DepartmentVo childVo = new DepartmentVo();
                childVo.setDepname(dept.getDepname());
                childVo.setDepcode(dept.getDepcode());

                departmentVo.getChildren().add(childVo);
            });

            resultList.add(departmentVo);
        }

        return resultList;
    }
}
